// demo of RequireSiteAdministrator

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Security;

using System.Threading;

//BAS.Dovas.ApplicationPages
namespace BAS.Dovas.WebControls
{

    public class DateTimeSecFieldEditControl : UserControl, IFieldEditor
    {


        protected CheckBox NoneDefault;
        protected CheckBox TodayDefault;
        protected CheckBox SelDateDefault;
        protected CheckBox SelDateDefaultFormula;

        protected DateTimeControl TheDatePicker;

        protected TextBox FormulaTxt;

        
        private void Setup(DovasFieldDateTimeSec field)
        {
            try
            {
                TheDatePicker.SelectedDate = DateTime.Now;
                if (field.DefaultValue == "" || field.DefaultValue == null)
                    NoneDefault.Checked = true;
                else if (field.DefaultValue != "")
                {
                    if (field.DefaultValue == "[today]")
                        TodayDefault.Checked = true;
                    else
                    {
                        SelDateDefault.Checked = true;
                        TheDatePicker.SelectedDate = DateTime.Parse(field.DefaultValue);
                    }
                }
                if (field.DefaultFormula != null)
                {
                    SelDateDefaultFormula.Checked = true;
                    FormulaTxt.Text = field.DefaultFormula;
                }
                else
                    NoneDefault.Checked = true;

               
            }
            catch { }

        }
        public void InitializeWithField(SPField field)
        {

            DovasFieldDateTimeSec fld = field as DovasFieldDateTimeSec;
            if (fld != null && !IsPostBack)
            {
                Setup(fld);
            }
            else if (fld == null && !IsPostBack)
            {
                //GetLists();
                ViewState["IsNew"] = true;
            }

        }

        
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            SetDateTimeContr();
        }

        private void SetDateTimeContr()
        {
            SPRegionalSettings ResSet = SPContext.Current.RegionalSettings;

            TheDatePicker.DateOnly = false;
            TheDatePicker.LocaleId = (int)ResSet.LocaleId;
            TheDatePicker.TimeZoneID = ResSet.TimeZone.ID;
            TheDatePicker.UseTimeZoneAdjustment = true;
            TheDatePicker.HijriAdjustment = ResSet.AdjustHijriDays;
            TheDatePicker.HoursMode24 = ResSet.Time24;
            TheDatePicker.FirstDayOfWeek = (int)ResSet.FirstDayOfWeek;
            TheDatePicker.FirstWeekOfYear = ResSet.FirstWeekOfYear;
            TheDatePicker.ShowWeekNumber = ResSet.ShowWeeks;
            
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            RegisterJavaScript();
        }

        public void OnSaveChange(SPField field, bool isNew)
        {
            DovasFieldDateTimeSec fld = field as DovasFieldDateTimeSec;
            if (fld != null)
            {

                field.DefaultValue = null;
                field.DefaultFormula = null;

                if (TodayDefault.Checked)
                    field.DefaultValue = "[today]";
                else if (SelDateDefault.Checked)
                    field.DefaultValue = TheDatePicker.SelectedDate.ToString();
                else if (SelDateDefaultFormula.Checked)
                    field.DefaultFormula = FormulaTxt.Text;

            }

        }
        public bool DisplayAsNewSection
        {
            get
            {
                return true;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
           // if (!IsPostBack) GetLists();
        }

        private void RegisterJavaScript()
        {
//            if (this.Page.ClientScript.IsClientScriptBlockRegistered("AddLinkScript") == false)
//            {
//                string newsScript = @"<script language='javascript' type='text/javascript'>
//                                       function change(obj)
//                                        {
//                                            document.getElementById('" + LinkTypeTxt.ClientID + @"').value=obj.options[obj.selectedIndex].value;
//                                        }
//                                        function changeBack(obj)
//                                        {
//                                            document.getElementById('" + BackLinkTypeTxt.ClientID + @"').value=obj.options[obj.selectedIndex].value;
//                                        }
//                                    </script>";
//                this.Page.ClientScript.RegisterClientScriptBlock(typeof(ObjectLinksField), "AddLinkScript", newsScript);
//            }
        }

        
        
    }
}
